<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 头部 -->
      <PageTool>
        <template #before>
          <el-button type="primary" @click="addFn">添加分类</el-button>
        </template>
        <template #after>
          <el-select v-model="agent" placeholder="全部经纪人" style="width:120px">
            <el-option v-for="item in jingJiRlists" :key="item.id" :label="item.name" :value="item.name">{{ item.name }}</el-option>
          </el-select>
          <el-input v-model="isKeyword" placeholder="请输入关键字">
            <el-button slot="append" icon="el-icon-search" @click="search" />
          </el-input>
        </template>
      </PageTool>
      <!-- 底部 -->
      <el-table :data="list" border>
        <!-- <el-table-column prop="image" label="图片" sortable="" /> -->
        <el-table-column prop="image" label="图片" sortable="">
          <template v-slot="{row}">
            <img
              v-imgError="require('@/assets/common/bigUserHeader.png')"
              style="width: 100px"
              :src="row.image"
              alt=""
            >
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="agent.name" label="经纪" />
        <el-table-column prop="wechat" label="微信" />
        <el-table-column prop="desc" label="介绍" />
        <el-table-column prop="" label="操作" width="200px">
          <template v-slot="{row}">
            <el-button size="small" type="primary" @click="editFn(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-card>
        <el-row type="flex" justify="center">
          <el-pagination
            current-page.sync="page"
            :page-size="pageSize"
            layout=" total,prev, pager, next"
            :total="total"
            @current-change="paging"
          />
        </el-row>
      </el-card>

      <addFn ref="form" :show-dialog.sync="showDialog" @addEvent="geiClientLists" />
    </div>
  </div>
</template>

<script>
import { geiClientList, delClient, getClientotal } from '@/api/clientAdmin'
import addFn from './components/addFn.vue'
export default {
  components: {
    addFn
  },
  data() {
    return {
      list: [],
      limit: 10,
      start: 0,
      showDialog: false,
      page: 2, // 当前页码,
      total: 0, //
      pageSize: 4,
      jingJiRlists: [], // 经纪人列表
      isKeyword: null, // 模糊搜索 传空字符串也是字符串后台查找空字符串 放unll 传给后台的就是空
      agent: null // 经纪人
    }
  },
  created() {
    this.geiClientLists()
  },
  methods: {
    // 分页跳转
    async paging(val) {
      const res = await geiClientList({
        _limit: this.pageSize,
        // 一次拿两条从点击的哪一页val开始
        _start: this.pageSize * (val - 1)
      })
      this.list = res.data
    },
    // 搜索
    async search() {
      // 未完成
      const res = await this.geiClientLists()
      console.log(res)
    },
    // 删除
    delFn(id) {
      this.$confirm('确定永久删除么', {
        type: 'warning'
      }).then(async() => {
        await delClient(id)
        // 重新刷新列表
        await this.geiClientLists()
      }).catch(() => {
        console.log('失败')
      })
    },
    // 编辑
    async editFn(id) {
      this.showDialog = true
      // 调用子组件回显函数
      this.$refs.form.getIdClients(id)
    },
    // 封装渲染函数
    async geiClientLists() {
      const res = await geiClientList({
        _limit: this.pageSize,
        _start: this.start,
        name_contains: this.isKeyword,
        agent: this.agent
      })
      // console.log(333, res)
      // 数据传入渲染列表
      this.list = res.data
      const rea = await getClientotal()
      // 显示总条数
      this.total = rea.data
      //  经纪人列表
      this.jingJiRlists = this.$refs.form.jingJiRlist
      // console.log(134, this.jingJiRlists)
    },
    // 添加分类
    addFn() {
      this.showDialog = true
    }
  }
}
</script>

<style  lang="scss" scoped>
.el-table{
    margin-top: 20px;
}
</style>
